<?php


class Crawl_sina_hot_from_html_library  extends Crawl_keyword_library{

    private $_header;
    private $_userAgent;
    private $_url;
    private $_cookie;
    private $_parameters = '';
    public  $_htmlsearch_config;

    /**
     * 定义 CURL 消息头、用户端信息、抓取地址
     * 定义 新浪网页抓取选项
     */

   function __construct(){
        parent::__construct();
        $openplatforms = $this->_ci->config->item('openplatforms');
        $this->_htmlsearch_config  = $openplatforms['openplatforms']['htmlsearch']['sina_phone'];
        $this->_null_tab    = $this->_htmlsearch_config['null'];

        $this->_header      = $this->_htmlsearch_config['header'];
        $this->_userAgent   = $this->_htmlsearch_config['user_agent'];
        $this->_url         = $this->_htmlsearch_config['url'];

        $this->write_log_stary_tag();
    }
    


    public function crawl($retry = FALSE) {
    	
    	$crawl_result = $this->try_crawl($retry);

        if(is_array($crawl_result) && array_key_exists($this->_error_string, $crawl_result)) {
            return $crawl_result;
        } else if(is_array($crawl_result) && array_key_exists('data', $crawl_result) && is_null($crawl_result['data'])) {
            return $crawl_result;
        } else {
            return $this->make_data($crawl_result);
        }
    }

    private function make_data($result = NULL) {
				
        /*$this->_ci->load->model('split/htmlsearchparser_model');
        $split_data = $this->_ci->htmlsearchparser_model->parser_sina_phone($this->_htmlsearch_config, $result);
        
        if($split_data == FALSE) {
            $error = $this->_ci->error_code_library->getError('make_data', 'data', 'match_split_data');
            log_message(self::LOG_LEVEL_WARING, json_encode($error), FALSE, $this->_log_dir);
            return $error;
        }*/
    	
    	//$data = json_decode($result, true);
    	//$split_data = $data['cards'][0]['card_group'];
        //return $split_data;
    	
    	$this->_ci->load->model('split/hotparser_model');	
    	$split_data = $this->_ci->hotparser_model->sina_hot_html_parser($result) ;
    	
    	//return  $result;      //这里打开测试是否能获取网页源码

    	if($split_data == FALSE) {
    		$error = $this->_ci->error_code_library->getError('make_data', 'data', 'match_split_data');
    		log_message(self::LOG_LEVEL_WARING, json_encode($error), FALSE, $this->_log_dir);
    		return $error;
    	}
    	
        return $split_data;
        
    }

    /**
     * 尝试抓取
     * @param bool $retry 是否使用代理和重试功能
     * @return array|string 错误时返回error数组 正确时返回抓取到的字符穿
     */

    private function try_crawl($retry = FALSE) {
        //临时数组
        $temp_array = array();
        //定义抓取参数
        $parameters = $this->make_parameters();
        
        if(is_array($parameters) && array_key_exists($this->_error_string, $parameters)) {
            log_message($this->_error_string, 'Crawl From Sina Wap Make CURL Parameters Error', FALSE, $this->_log_dir);
            return $parameters;
        }
        if($retry == FALSE) {
            //不使用代理、不重试直接抓取
            log_message(self::LOG_LEVEL_NOTICE, 'Will Crawl From Sina Wap Un Use Retry And Un Use Proxy With Parameters: '.json_encode($this->_parameters), FALSE, $this->_log_dir);
            return self::get_content($this->_parameters);
        } else {
            $crawl_use_proxy_ip_result = $this->crawl_use_proxy_ip($this->_parameters, self::PRROXY_IP, $this->_crawl_from['sina_phone'], TRUE);
            if(is_array($crawl_use_proxy_ip_result)) {
                if(!array_key_exists($this->_error_string, $crawl_use_proxy_ip_result)) {
                    $temp_array = $crawl_use_proxy_ip_result;
                }

                $re_crawl_result = $this->crawl_use_proxy_ip($this->_parameters, self::MOBILITY_IP, $this->_crawl_from['sina_phone'], TRUE);

                if(is_array($re_crawl_result) && !array_key_exists($this->_error_string, $re_crawl_result) && !array_key_exists($this->_null_data_array_key, $re_crawl_result)) {
                    return $this->make_crawl_return_array($temp_array, $re_crawl_result);
                } else if(count($temp_array) > 0) {
                    return $this->make_crawl_return_array($temp_array);
                }else {
                    return $re_crawl_result;
                }
            } else {
                return $crawl_use_proxy_ip_result;
            }
        }
    }

    /**
     * 抓取内容验证
     * @param $html
     * @return bool
     */
    public function match_html($html) {
    	
        if(strpos($html, $this->_htmlsearch_config['no_login'])) {
            
            //如果返回登陆页 重新组织参数 更新COOKIE
            $this->make_parameters();
            
            //$this->label_cookie($this->_cookie['id']);
            return $this->_ci->error_code_library->getError('curl', 'data', 'cookie_error', $this->_htmlsearch_config['no_login']);
        } elseif(strpos($html, $this->_htmlsearch_config['result_error'])) {
            $this->label_proxy_table($this->_proxy_ip, $this->_use_proxy);
            return $this->_ci->error_code_library->getError('curl', 'data', 'verification_code', $this->_htmlsearch_config['result_error']);
        } elseif(strpos($html, $this->_htmlsearch_config['result_success']) === FALSE) {
            return $this->_ci->error_code_library->getError('curl', 'data', 'incomplete_answers', $this->_htmlsearch_config['result_success']);
        } else {
            return TRUE;
        }

    }


    private function make_parameters() {
    		
    	$post_data=$this->_ci->input->post();   
        $url='';

	    if(!empty($post_data['dateType']))
		{
                $subject='http://d.weibo.com/?feed_sort=102803_ctg1_dateType_-_ctg1_dateType&feed_filter=102803_ctg1_dateType_-_ctg1_dateType' ;
				$url=str_replace('dateType', $post_data['dateType'], $subject) ;
				$url=$url.'&page='.$post_data['page'] ;
		}
		else
		{
        	if($post_data['cat']==='default')
        	{
        		$url='http://d.weibo.com/' ;
        	}
        	else
        	{
        		$subject='http://d.weibo.com/102803_ctg1_cat_-_ctg1_cat' ;
        		$url=str_replace('cat', $post_data['cat'], $subject) ;
        	}
        	$url = $url . '?page='.$post_data['page'] ;  
		}

        //$url='http://weibo.com/p/1008087bef7c8c2cd3e28667d7cbf6ed9a7c4b?k=%E5%A4%A9%E6%B4%A5%E5%A1%98%E6%B2%BD%E5%A4%A7%E7%88%86%E7%82%B8&from=trendtop_api' ;
        // 热门话题也可以抓取     
        //$url='http://weibo.com/zhouhongyi?from=feed&loc=avatar' ;
		//$url='http://d.weibo.com/102803_ctg1_4688_-_ctg1_4688?from=faxian_hot&mod=fenlei#';
        
        $parameters['url'] = $url ;
        $parameters['method'] = self::METHOD_GET;
        
        /*$cookie = $this->get_cookie();
			
		if(is_array($cookie) && array_key_exists($this->_error_string, $cookie)) {
			return $cookie;
		}
		
		$this->_cookie = $cookie;
		
		$parameters['cookie_str'] = true;
		$parameters['cookie'] = $this->_cookie['cookie_str'];
		
		$this->update_cookie_use_count($cookie['id']);*/
        
        $parameters['cookie_str'] = true;
        $parameters['cookie'] = "YF-Page-G0=1ac418838b431e81ff2d99457147068c; SUB=_2AkMhLEVpf8NjqwJRmPodzGznb4x-ywzEiebDAHzsJxIxHkhO7HlZ--_kZTAQVeg10CRAms9G_OfP; SUBP=0033WrSXqPxfM72-Ws9jqgMF55z29P9D9WW4Agg7vFrJvu4.O_vrCUjX; _s_tentry=passport.weibo.com; Apache=7264674508478.492.1450232415008; SINAGLOBAL=7264674508478.492.1450232415008; ULV=1450232415024:1:1:1:7264674508478.492.1450232415008:";
        
        return $this->_parameters = $parameters;
        
        
    }

}
